﻿using DevExpress.XtraEditors;
using MDK.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmDeskOperation : BaseForm
    {
        private int position = 0;
        private int _Position { get { return position; } set { position = value; } }
        private Dictionary<int, int> posDic = new Dictionary<int, int>(); //1:空工位 2:空托 3:阳性 4:无条码 5:正常
        public frmDeskOperation()
        {
            InitializeComponent();
        }

        public frmDeskOperation(string Message) :this()
        {
          
        }

        private void frmDeskOperation_Load(object sender, EventArgs e)
        {
            InitPalletStatus();
            //lbMessage.Invoke(new MethodInvoker(() => { lbMessage.Text = Message; }));
            //splashScreenManager1.ShowWaitForm();
            //if (App.flow.BeginManaulHandle())
            //{
            //    splashScreenManager1.CloseWaitForm();
            //}
        }

        /// <summary>
        /// 初始化托状态
        /// </summary>
        public void InitPalletStatus()
        {
            //工位
            for (int i = 0; i < 40; i++)
            {
                SetPositionStatus(i);
            }
        }

        /// <summary>
        /// 设置单个工位状态
        /// </summary>
        /// <param name="position"></param>
        public void SetPositionStatus(int position)
        {
            var pallet = App.flow.PosModel.DesktopPositions[position].Pallet;
            if (pallet == null)
            {
                posDic.Remove(position);
                SetNullPosition(position);
                posDic.Add(position, 1);
            }
            else
            {
                if (pallet.Tubes.Select(t => t.Type).All(t=>t == TubeTypes.None)) 
                {
                    posDic.Remove(position);
                    SetNullPallet(position);
                    posDic.Add(position, 2);
                }
                else
                {
                    if (pallet.Tubes.All(t => t.Type == Device.TubeTypes.Positive))
                    {
                        posDic.Remove(position);
                        SetPositivePosition(position);
                        posDic.Add(position, 3);
                    }
                    //无条码
                    else if (pallet.NoBarcodeCount > 0)
                    {
                        posDic.Remove(position);
                        SetNoCodePosition(position);
                        posDic.Add(position, 4);
                    }
                    else
                    {
                        posDic.Remove(position);
                        SetNormal(position);
                        posDic.Add(position, 5);
                    }
                }
            }
        }

        /// <summary>
        /// 显示右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureEdit_Click(object sender, EventArgs e)
        {
            var controlname = (sender as PictureEdit).Name;
            int pos = 0;
            int.TryParse(controlname.Substring(11, controlname.Length - 11), out pos);
            position = pos - 1;
            contextMenuStrip.Show(Control.MousePosition);
        }

        /// <summary>
        /// 补空托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 补空托ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            App.flow.PosModel.DesktopPositions[_Position].Pallet = new Device.Pallet(){State = PalletStatus.WaitToFreezer};
            App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes = Enumerable.Range(0, 20).Select(t => new Tube() { Type = TubeTypes.None }).ToArray();
            SetPositionStatus(_Position);
        }

        //private void 补冷藏托ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    frmOperationTube frm = new frmOperationTube(_Position);
        //    if (frm.ShowDialog() == DialogResult.Cancel)
        //    {
        //         SetPositionStatus(frm._Position);
        //    }
        //}

        /// <summary>
        /// 补条码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 补条码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmOperationNoCodeTube frm = new frmOperationNoCodeTube(_Position);
            if (frm.ShowDialog() == DialogResult.Cancel)
            {
                SetPositionStatus(frm._Position);
            }
        }

        /// <summary>
        /// 菜单显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            补空托ToolStripMenuItem.Visible = false;
            补条码ToolStripMenuItem.Visible = false;
            移除托ToolStripMenuItem.Visible = false;
            if (posDic[_Position] == 1)
            {
                补空托ToolStripMenuItem.Visible = true;
            }
            if (posDic[_Position] == 4)
            {
                补条码ToolStripMenuItem.Visible = true;
            }
            if (posDic[_Position] == 3 || posDic[_Position] == 2)
            {
                移除托ToolStripMenuItem.Visible = true;
            }
        }

        #region  设置状态
        public void SetNullPosition(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.空位;
        }

        public void SetNullPallet(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.空托;
        }

        public void SetPositivePosition(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.阳性;

        }

        public void SetNoCodePosition(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.无条码;
        }

        public void SetNormal(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.正常;
        }

        private void 移除托ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            App.flow.PosModel.DesktopPositions[_Position].Pallet = null;
            SetPositionStatus(_Position);
        }

        #endregion


        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void frmDeskOperation_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!App.flow.PosModel.DesktopPositions.Any(t => t.Pallet != null && t.Pallet.NoBarcodeCount > 0))
            {
                //App.flow.EndManaulHandle();
                e.Cancel = false;
            }
            else
            {
                FastMessageBox.Warnning("依然有无条码试管");
                e.Cancel = true;
            }
        }
    }
}
